package model.player;

import java.util.LinkedList;
import java.util.List;

import model.map.GameTile;
import model.playable.Playable;
import model.playable.structure.University;
import model.playable.structure.Structure;
import model.playable.unit.Worker;
import model.technology.PlayableTechnologyTree;

public class UniversityManager extends StructureManager
{
	private University[] structures;
	private int grabPointer;
	private int fillPointer;
	private int defAttMod, armorMod, hpMod, upkeepMod, visMod, infRadMod, prodRateMod;
	
	private PlayableTechnologyTree techTree;
	
	public UniversityManager(Player player, ResourceManager rm)
	{
		super(player, rm, "University");
		structures= new University[10];
		grabPointer = 0;
		fillPointer = 0;
		defAttMod = armorMod = hpMod = upkeepMod = visMod = infRadMod = prodRateMod = 0;
	}
	
	

	public void createNewStructure(GameTile tile, Worker[] workers)
	{
		if (isFull())
			throw new RuntimeException("Cannot add any more University structures");
		
		University s = new University(super.getPlayer(), workers);
		s.incrementArmor(armorMod);
		s.incrementDefensiveDamage(defAttMod);
		s.incrementHealth(hpMod);
		s.incrementInfluenceRadius(infRadMod);
		s.incrementProductionRate(prodRateMod);
		s.incrementUpkeep(upkeepMod);
		s.incrementVisibilityRadius(visMod);
		
		
		goToNextFillSpot();
		s.setIdNumber(fillPointer);
		
		structures[fillPointer++] = s;
		
		super.addStructureToMap(tile, s);
	}

	private void goToNextFillSpot()
	{
		if (isEmpty())
		{
			fillPointer = 0;
			return;
		}
		
		while (structures[fillPointer%structures.length] != null )
		{
			fillPointer = (fillPointer +1) % structures.length;
		}
		
	}



	@Override
	public void payUpkeep() 
	{
		for (Structure s: structures)
			if (s != null)
			{
				s.addUpkeep(super.grabFood(s.getUpkeep()));
			}
		
	}
	
	public void remove(int i)
	{
		if (structures[i] != null)
			super.removeFromMap(structures[i]);
		structures[i] = null;
	}

	@Override
	public Playable getCurPlayable() 
	{
		
		while (structures[grabPointer] == null)
		{
			grabPointer = (grabPointer +1) % structures.length;
		}
		return structures[grabPointer];
	}

	@Override
	public Playable getNextPlayable() 
	{
		grabPointer = (grabPointer +1) % structures.length;
		
		while (structures[grabPointer] == null)
		{
			grabPointer = (grabPointer +1) % structures.length;
		}
		return structures[grabPointer];
	}

	@Override
	public Playable getPrevPlayable() 
	{		
		grabPointer = (grabPointer -1 + structures.length) % structures.length;
		
		while (structures[grabPointer] == null)
		{
			grabPointer = (grabPointer -1 + structures.length) % structures.length;
		}
		return structures[grabPointer];
	}

	@Override
	public void upgradeArmorStat()
	{
		armorMod++;
		
		for (University s : structures)
			if (s != null)
				s.incrementArmor(1);
	}

	@Override
	public void upgradeDefAttackStat() 
	{
		defAttMod++;
		
		for (University s : structures)
			if (s != null)
				s.incrementDefensiveDamage(1);
	}

	@Override
	public void upgradeHealthStat() 
	{
		hpMod++;
		
		for (University s : structures)
			if (s != null)
				s.incrementHealth(1);
	}

	@Override
	public void upgradeProductionRateStat() 
	{
		prodRateMod++;
		
		for (University s : structures)
			if (s != null)
				s.incrementProductionRate(1);
	}

	@Override
	public void upgradeUpkeepStat() {
		upkeepMod++;
		
		for (University s : structures)
			if (s != null)
				s.incrementUpkeep(1);
	}

	@Override
	public void upgradeWorkerRadiusStat() 
	{
		for (Structure s : structures)
			if (s != null)
				/*s*/;
	}

	@Override
	public void upgradeWorkerDensityStat() {
		// TODO Auto-generated method stub

	}


	@Override
	public void upgradeInfluenceRadiusStat() 
	{
		infRadMod++;
		
		for (University s : structures)
			if (s != null)
				s.incrementInfluenceRadius(1);
	}


	@Override
	public void upgradeVisibilityStat() 
	{
		visMod++;
		
		for (University s : structures)
			if (s != null)
				s.incrementVisibilityRadius(1);
	}



	@Override
	public boolean isEmpty() 
	{
		for (Structure s : structures)
			if (s != null)
				return false;
		
		return true;
	}



	@Override
	public boolean isFull()
	{
		for (Structure s : structures)
			if (s == null)
				return false;
		
		return true;
	}



	@Override
	public Playable getPlayableID(int id) 
	{
		return structures[id];
	}

	@Override
	public void update()
	{
		for (Structure s: structures)
			if (s != null)
				s.executeCommand();
		
		payUpkeep();
	}

	@Override
	public List<Structure> getAllStructures() 
	{
		List<Structure> result = new LinkedList<Structure>();
		
		for (Structure s : structures)
			if (s != null)
				result.add(s);
		
		return result;
	}



	public void setTechTree(PlayableTechnologyTree techTree)
	{
		this.techTree = techTree;
		
	}
}